package com.example.cmtwo.controller;


import com.example.cmtwo.bean.Commodity;
import com.example.cmtwo.service.AdminCommodityservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


import java.text.SimpleDateFormat;
import java.util.*;

@Controller
public class AdminCommoditycontroller {
    @Autowired
    private AdminCommodityservice adminCommodityservice;

    private Commodity commodity;

    @ResponseBody
    @RequestMapping(value = "/cominformation",method = RequestMethod.GET)
    public Map<Integer,Map> getallcommodity()
    {
        List<Commodity> com = new LinkedList<Commodity>();
        Map<Integer,Map> list = new HashMap<Integer, Map>();
        com = adminCommodityservice.getCommodityByall();
        Integer item=0;

        for (Iterator i = com.iterator();i.hasNext();)
        {
            Map<String,String> map = new HashMap<String, String>();
            commodity = (Commodity) i.next();
            map.put("Cid", commodity.getCid().toString());
            map.put("Uid",commodity.getUid().toString());
            map.put("Cname", commodity.getCname());
            map.put("Price", commodity.getPrice().toString());
            map.put("Originalprice", commodity.getOriginalprice().toString());
            String data = new SimpleDateFormat("yyyy-MM-dd").format(commodity.getPublicdata());
            map.put("Publicdata",data);
            if (commodity.getOffdata()!=null)
            {
                String data2 = new SimpleDateFormat("yyyy-MM-dd").format(commodity.getOffdata());
                map.put("Offdata",data2);
            }
            else {
                String data2 = "未售出";
                map.put("Offdata",data2);
            }
            map.put("Image", commodity.getImage());
            map.put("Intor", commodity.getIntro());
            map.put("Classification",commodity.getClassification());
            list.put(item,map);
            item++;
        }

        return list;
    }
}
